package Tabs;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;

import MainMethods.FileWr;
import MainMethods.HoverImage;
import MainMethods.Screenshot;

public class PlayGames {
	HoverImage hover = new HoverImage();
	Screenshot scsh = new Screenshot();

	public void testGames(WebDriver driver, FileWr file, String screenDir)
			throws ElementNotVisibleException, NoSuchElementException {
		// Can you play a html5 game? Can you search?
		// open Games tab
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		driver.findElement(By.id("games_tab")).click();
		try {
			Thread.sleep(4000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if (driver.getCurrentUrl().equals(
				"http://staging.membersarea.jomediainc.com/#games")) {
			file.writeFile("Select games tab - PASSED");
		} else {
			file.writeFile("Select games tab - FAILED");
		}
		// Go to the search bar (upper), Search a HTML game (eg. Roulette)
		driver.findElement(By.id("search")).sendKeys("Roulette");
		// click button don't work in IE
		driver.findElement(By.id("search_button")).click();
		// try {
		// Robot robot1 = new Robot();
		// robot1.keyPress(KeyEvent.VK_ENTER);
		// } catch (AWTException e1) {
		// // TODO Auto-generated catch block
		// e1.printStackTrace();
		// }
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		b1: if (hover.doesElementExist(driver,
				By.xpath("//*[@id=\"browse_wrapper\"]")) == true) {
			// start game from search result page
//			Actions hov = new Actions(driver);
//			Actions playBtn = hov.moveToElement(driver.findElement(By
//					.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[3]/a/div[1]")));
//			playBtn.perform();
//			driver.findElement(
//					By.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[3]/div[1]/div"))
//					.click();
			driver.findElement(By
					.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[3]/a/div[1]")).click();
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			driver.findElement(By
					.xpath("//*[@id=\"title_download_container\"]/a")).click();
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			scsh.makeScreen("HTML5 Game - PASSED", screenDir);
			if (hover.doesElementExist(driver,
						By.xpath("//*[@id=\"screen_wrapper\"]")) == true) {
				file.writeFile("HTML5 Game (eg. Roulette) is opened and it can be played - PASSED");
			} else {
				file.writeFile("HTML5 Game (eg. Roulette) is opened and it can be played - FAILED");
			}
		} else {
			file.writeFile("Search a HTML game (eg. Roulette) - FAILED");
			break b1;
		}

		// Go to the search bar (upper), Search an android game (eg. Goosy
		// farmer android)
		driver.findElement(By.id("search")).clear();

		driver.findElement(By.id("search")).sendKeys("Goosy farmer");
		driver.findElement(By.id("search_button")).click();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if (hover.doesElementExist(driver,
				By.xpath("//*[@id=\"browse_wrapper\"]/div[2]")) == true) {
			// start game from search result page
//			Actions hov = new Actions(driver);
//			Actions playBtn = hov.moveToElement(driver.findElement(By
//					.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[3]")));
//			playBtn.perform();
//			driver.findElement(
//					By.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[3]/div[1]/div"))
//					.click();
			driver.findElement(By.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[3]/a/div[1]")).click();
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			driver.findElement(By.xpath("//*[@id=\"6571\"]")).click();
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			scsh.makeScreen("Android Game - PASSED", screenDir);
			try {
				Robot robot = new Robot();
				robot.keyPress(KeyEvent.VK_ESCAPE);
			} catch (AWTException e) {
				e.printStackTrace();
			}
			file.writeFile("Can you download an android game? Look screenshot - 'Android Game - PASSED.png'");
		} else {
			file.writeFile("Search an Android game (eg.  Goosy farmer) - FAILED");
		}
	}

}
